//! @Author: DengLibin
//! @Date: Create in 2024-07-17 15:09:29
//! @Description:

use my_trait::MustHaveTableName;

use sqlx::{Pool, MySql};

use crate::{
    entity::user::User,
    sys::global::{to_global_result, GlobalResult},
};

/// @Author: DengLibin
/// @Date: Create in 2024-07-19 17:19:41
/// @Description: 登录名计数
pub async fn count_user_login_name(
    user_login_name: &str,
    pool: &Pool<MySql>,
) -> GlobalResult<i64> {
    let r = sqlx::query_as::<MySql, (i64,)>(
        format!(
            "SELECT COUNT(1) FROM `{}` WHERE user_login_name = ? and deleted = false and status<>-1",
           
            User::table_name()
        )
        .as_str(),
    )
    .bind::<&str>(user_login_name)
    .fetch_one(pool)
    .await;
    let count = to_global_result(r)?;
    Ok(count.0)
}

pub async fn get_user_login_name(
    user_login_name: &str,
    pool: &Pool<MySql>,
) -> GlobalResult<Option<User>> {
    let r = sqlx::query_as::<MySql, User>(
        format!(
            "SELECT * FROM `{}` WHERE user_login_name =?  and deleted=false and status<>-1",
        
            User::table_name()
        )
        .as_str(),
    )
    .bind::<&str>(user_login_name)
    .fetch_optional(pool)
    .await;
    to_global_result(r)
}
